package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduCheckinMapper;
import com.zenith.edu.po.domain.EduCheckin;
import com.zenith.edu.po.dto.EduCheckinDTO;
import com.zenith.edu.po.vo.EduCheckinVO;
import com.zenith.edu.service.EduCheckinService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduCheckinServiceImpl extends ServiceImpl<EduCheckinMapper, EduCheckin> implements EduCheckinService {
    
    @Override
    public EduCheckinVO createCheckin(EduCheckinDTO dto) {
        EduCheckin checkin = new EduCheckin();
        BeanUtils.copyProperties(dto, checkin);
        this.save(checkin);
        
        EduCheckinVO vo = new EduCheckinVO();
        BeanUtils.copyProperties(checkin, vo);
        return vo;
    }
    
    @Override
    public EduCheckinVO getCheckinById(Long id) {
        EduCheckin checkin = this.getById(id);
        if (checkin == null) {
            return null;
        }
        
        EduCheckinVO vo = new EduCheckinVO();
        BeanUtils.copyProperties(checkin, vo);
        return vo;
    }
    
    @Override
    public EduCheckinVO updateCheckin(EduCheckinDTO dto) {
        EduCheckin checkin = new EduCheckin();
        BeanUtils.copyProperties(dto, checkin);
        this.updateById(checkin);
        
        EduCheckinVO vo = new EduCheckinVO();
        BeanUtils.copyProperties(checkin, vo);
        return vo;
    }
    
    @Override
    public boolean deleteCheckin(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduCheckinVO> getCheckinList() {
        List<EduCheckin> checkinList = this.list();
        return checkinList.stream().map(checkin -> {
            EduCheckinVO vo = new EduCheckinVO();
            BeanUtils.copyProperties(checkin, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}